#include<bits/stdc++.h>
#define ll long long
using namespace std;

int n,a[200005];
ll f[2005][2005],ans;

void sol()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    ans=0;
    memset(f,0,sizeof(f));
    for(int i=0;i<=n;i++)
    {
        for(int j=0;j<=n;j++)
        {
            if(i==j) continue;
            int t=max(i,j)+1;
            f[i][t]=max(f[i][t],f[i][j]+(a[j]==a[t])*a[t]);
            f[t][j]=max(f[t][j],f[i][j]+(a[i]==a[t])*a[t]);
            ans=max(ans,f[i][j]);
        }
    }
    printf("%lld\n",ans);
}

int main()
{
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    int t;
    scanf("%d",&t);
    // t=5;
    while(t--) sol();
    return 0;
}
// Code by Jorisy. Best Wishes & Good Luck.